package com.zby.springbootdemo.exception;

/**
 * @Author zhangbingyue
 * @Description //TODO 测试类$
 * @Date $ $
 **/


//    1. 异常捕捉例子
//public class Test {
//    @Autowiredprivate
//    IAddressService addressService;
//

import jdk.nashorn.internal.objects.annotations.Getter;
import jdk.nashorn.internal.objects.annotations.Setter;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * 添加收货地址 * @param addressDTO * @return
 *
 * @Valid：用于验证所传实体类是否符合注解验证要求 ：
 * 参考  https://blog.csdn.net/weixin_38118016/article/details/80977207
 */
//    @RequestMapping(method = RequestMethod.POST)
//    public AddressDTO add(@Valid @RequestBody AddressDTO addressDTO) {
//        Address address = new Address();
//        BeanUtils.copyProperties(addressDTO, address);
//        Address result;
//        try {
//            result = addressService.createAddress(addressDTO.getUid(), address);
//        } catch (NotFindUserException e) {
//            throw new ApiNotFindUserException("找不到该用户");
//        } catch (Exception e) {//未知错误        throw new ApiException(e);    }    AddressDTO resultDTO = new AddressDTO();    BeanUtils.copyProperties(result,resultDTO);    resultDTO.setUid(result.getUser().getId());     return resultDTO;}
//        }
//    }

//}


//2 .将异常信息转换为可识别对象
// @ControllerAdvice：用于拦截Controller的接口，比如当接口抛出异常时，可以被拦截，然后返回指定的报文
// @ControllerAdvice(annotations = RestController.class)：指向所有带有注解@RestController的控制器
// @ControllerAdvice("org.example.controllers") ：指向所有指定包中的控制器
// @ControllerAdvice(assignableTypes = {ControllerInterface.class, AbstractController.class}) ：指向所有带有指定签名的控制器

//@ControllerAdvice(annotations = RestController.class)
//public class Test {
//    /**
//     * Handle exceptions thrown by handlers.
//     */
//    @ExceptionHandler(value = Exception.class)
//    @ResponseBody
//    public ResponseEntity<ErrorDTO> exception(Exception exception, HttpServletResponse response) {
//        ErrorDTO errorDTO = new ErrorDTO();
//        if (exception instanceof ApiExceptionUtil) {//api异常
//            ApiExceptionUtil apiException = (ApiExceptionUtil) exception;
//            errorDTO.setErrorCode(apiException.getErrorCode());
//        } else {//未知异常
//            errorDTO.setErrorCode(0L);
//        }
//        errorDTO.setTip(exception.getMessage());
//        ResponseEntity<ErrorDTO> responseEntity = new ResponseEntity<>(errorDTO, HttpStatus.valueOf(response.getStatus()));
//        return responseEntity;
//    }
//
//    @Setter
//    @Getter
//    class ErrorDTO {
//        private Long errorCode;
//        private String tip;
//    }
//}